package com.zcl.demo.concurrent;

import java.util.concurrent.CyclicBarrier;

/**
 * @author 周成龙
 * @date 2021-01-28 12:53
 * description
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier barrier=new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                System.out.println("子任务完成，现在执行合并任务");
            }
        });
        new Thread(){
            @Override
            public void run() {
                try {
                    System.out.println("执行子任务1");
                    barrier.await();
                    System.out.println("合并任务执行完成，退出子任务1");
                }catch (Exception e){
                    System.out.println(e.toString());
                }

            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                try {
                    System.out.println("执行子任务2");
                    barrier.await();
                    System.out.println("合并任务执行完成，退出子任务2");
                }catch (Exception e){
                    System.out.println(e.toString());
                }

            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                try {
                    System.out.println("执行子任务3");
                    barrier.await();
                    System.out.println("合并任务执行完成，退出子任务3");
                }catch (Exception e){
                    System.out.println(e.toString());
                }

            }
        }.start();
    }
}
